package com.lmonkey.servlet.product;

import com.jspsmart.upload.*;
import com.lmonkey.dao.Basedao;
import com.lmonkey.shop.PRODUCT;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MultipartConfig
@WebServlet("/admin/admin_doproductadd")
public class DoProductAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //创建SmartUpload的对象
//        SmartUpload su = new SmartUpload();
//        su.setContentDisposition("UTF-8");
//        //初始化
//        su.initialize(this.getServletConfig(),request,response);
//        //上传过程
//        try {
//            su.upload();
//        } catch (SmartUploadException e) {
//            e.printStackTrace();
//        }
//        //获取上传的文件对象
//        Files fs = su.getFiles();
//        File f = fs.getFile(0);
//
//        //获取上传文件的名称
//        String fname = f.getFileName();
//        //保存路径
//        try {
//            su.save("D:/java/obj/web/shangc/image/product");
//        } catch (SmartUploadException e) {
//            e.printStackTrace();
//        }
//        //获取表单数据
//        Request reql = su.getRequest();
//        String pname = new String(reql.getParameter("productname").getBytes("gb2312"),"UTF-8");
//        String id = reql.getParameter("parentid");
//        String price = reql.getParameter("productprice");
//        String desc = new String(reql.getParameter("productDesc").getBytes("gb2312"),"UTF-8");
//        String stock = reql.getParameter("productstock");


        //获取数据
        String pname = request.getParameter("productname");
        System.out.println(pname);
        String id = request.getParameter("parentid");
        Float price = Float.parseFloat(request.getParameter("productprice"));
        String desc = request.getParameter("productDesc");
        String stock = request.getParameter("productstock");
        //获取Part对象
        Part pare = request.getPart("photo");
        String fname = pare.getSubmittedFileName();
        System.out.println("文件名："+fname);
        //获取文件存储路径
        String filepath = "D:\\java\\obj\\web\\shangc\\image\\product";
        System.out.println(filepath);
        //上传文件
        pare.write(filepath+"\\"+fname);

        System.out.println(pname+"**"+desc);
        PRODUCT p = new PRODUCT(0,pname,desc,price,Integer.parseInt(stock),Integer.parseInt(id.split("-")[0]),Integer.parseInt(id.split("-")[1]),fname);

        int count = 0;
        Connection conn = Basedao.getconn();
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            String sql = "insert into lmonkey_product values(null ,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setObject(1,p.getProduct_name());
            ps.setObject(2,p.getProduct_description());
            ps.setFloat(3,p.getProduct_price());
            ps.setInt(4,p.getProduct_stock());
            ps.setInt(5,p.getProduct_fid());
            ps.setInt(6,p.getProduct_cid());
            ps.setObject(7,p.getProduct_filename());
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (count>0){
            response.sendRedirect("admin_productselect");
        }else {
            PrintWriter out = response.getWriter();
            out.println("<script>alert('商品添加失败');location.href='/lyq/admin/admin_toproductadd';</script>");
            out.close();
        }
    }
}